package cn.edu.zut.salarymanagesystem.activities;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import cn.edu.zut.salarymanagesystem.R;
import cn.edu.zut.salarymanagesystem.config.Storage;
import cn.edu.zut.salarymanagesystem.utils.Browser;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ResetPasswordActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvOldPassword;
    private TextView tvNewPassword;
    private EditText editOldPassword;
    private EditText editNewPassword;
    private Button btnReset;
    private static final byte NETWORK_ERROR = 0x00;
    private static final byte RESET = 0x01;
    private static final byte PARAMETER_ERROR = 0x02;
    private static final byte SEVER_ERROR = 0x03;
    private static final byte USER_ERROR = 0x04;
    private static final byte ERROR = 0x05;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case NETWORK_ERROR:
                    Toast.makeText(getApplicationContext(), "请求失败！请检查网络设置", Toast.LENGTH_SHORT).show();
                    break;
                case RESET:
                    Toast.makeText(getApplicationContext(), "修改密码成功", Toast.LENGTH_SHORT).show();
                    break;
                case PARAMETER_ERROR:
                    Toast.makeText(getApplicationContext(), "参数不足", Toast.LENGTH_SHORT).show();
                case SEVER_ERROR:
                    Toast.makeText(getApplicationContext(), "服务器错误", Toast.LENGTH_SHORT).show();
                    break;
                case USER_ERROR:
                    Toast.makeText(getApplicationContext(), "用户不存在或原密码错误", Toast.LENGTH_SHORT).show();
                    break;
                case ERROR:
                    Toast.makeText(getApplicationContext(), "出错了", Toast.LENGTH_SHORT).show();

            }
        }
    };

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        findViewById(R.id.btn_back).setOnClickListener(v -> this.finish());
        tvOldPassword = findViewById(R.id.tv_old_password);
        tvNewPassword = findViewById(R.id.tv_new_password);
        editOldPassword = findViewById(R.id.edit_old_password);
        editNewPassword = findViewById(R.id.edit_new_password);
        btnReset = findViewById(R.id.btn_reset);
        btnReset.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String oldPasswd = editOldPassword.getText().toString();
        String newPasswd = editNewPassword.getText().toString();
        RequestBody formBody = new FormBody.Builder()
                .add("uid", Storage.currentUser.getUid())
                .add("oldPass", oldPasswd)
                .add("newPass", newPasswd)
                .build();
        Request request = new Request.Builder()
                .url(Storage.url + "/user/change")
                .post(formBody)
                .build();
        OkHttpClient client = Browser.getClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                handler.sendEmptyMessage(NETWORK_ERROR);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String result = response.body().string();
                Log.i("d8g", result);
                switch (result) {
                    case "-1":
                        handler.sendEmptyMessage(PARAMETER_ERROR);
                        break;
                    case "1":
                        handler.sendEmptyMessage(RESET);
                        finish();
                        break;
                    case "0":
                        handler.sendEmptyMessage(SEVER_ERROR);
                        break;
                    case "2":
                        handler.sendEmptyMessage(USER_ERROR);
                        break;
                    default:
                        //出错了，异常情况，自行处理
                        handler.sendEmptyMessage(ERROR);
                }
            }
        });
    }
}